<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索框 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="searchOrder"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchOrderBtn"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table :data="orderList.goods" style="width: 100%">
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="100px">
        </el-table-column>
        <el-table-column label="是否付款" width="120px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '1'" type="success"
              >已支付</el-tag
            ><el-tag v-else type="warning">未支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" width="120px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_send === '是'" type="success"
              >已发货</el-tag
            ><el-tag v-else type="warning">未发货</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="200px">
          <template slot-scope="scope">
            {{ scope.row.create_time | formatData }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              class="el-icon-edit"
              size="mini"
              @click="modOrderBtn(scope.row)"
            ></el-button>
            <el-button
              type="success"
              class="el-icon-location-information"
              size="mini"
              @click="kuaidiOrderBtn(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getOrderParams.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="getOrderParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="orderList.total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改订单对话框 -->
    <el-dialog title="修改订单" :visible.sync="modOrderDialog" width="50%">
      <el-form
        ref="orderListIDRef"
        :rules="orderListIDRules"
        :model="orderListID"
        label-width="80px"
      >
        <el-form-item label="订单编号">
          <el-input v-model="orderListID.order_number" disabled></el-input>
        </el-form-item>
        <el-form-item label="订单价格" prop="order_price">
          <el-input type="number" v-model="orderListID.order_price"></el-input>
        </el-form-item>
        <el-form-item label="是否发货">
          <el-switch v-model="is_send"></el-switch>
        </el-form-item>
        <el-form-item label="是否付款">
          <el-switch v-model="pay_status"></el-switch>
        </el-form-item>
        <el-form-item label="支付方式">
          <el-radio-group v-model="orderListID.order_pay">
            <el-radio label="0">未支付</el-radio>
            <el-radio label="1">支付宝</el-radio>
            <el-radio label="2">微信</el-radio>
            <el-radio label="3">银行卡</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modOrderDialog = false">取 消</el-button>
        <el-button type="primary" @click="subModOrder">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流对话框 -->

    <el-dialog title="物流进度" :visible.sync="kuaidiDialog" width="50%">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="kuaidiDialog = false">取 消</el-button>
        <el-button type="primary" @click="kuaidiDialog = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
/***
 * 1、搜索功能失败 关键字无法查询
 * 2、修改订单中是否支付失败 无法写入后台数据
 * 3、订单失败 无法查询后台接口
 * ***/
export default {
  data() {
    return {
      //  搜索订单号
      searchOrder: '',
      // 获取订单列表的参数
      getOrderParams: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 返回的订单列表数据
      orderList: {},
      // 是否显示修改订单对话框
      modOrderDialog: false,
      // 修改表单验证规则
      orderListIDRules: {},
      // 返回的ID订单列表数据
      orderListID: {
        is_send: 0,
        order_pay: '',
        order_price: 0,
        order_number: '',
        pay_status: 0
      },
      // 返回ID订单的id
      orderID: 0,
      // 返回订单的数据处理
      is_send: false,
      pay_status: false,
      // 快递进度对话框
      kuaidiDialog: false
    }
  },
  created() {
    this.getOrderLists()
  },
  methods: {
    //   获取订单列表
    async getOrderLists() {
      const { data } = await this.$http.get('orders', {
        params: this.getOrderParams
      })
      if (data.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      // this.$message.success('获取订单列表成功')
      console.log('获取订单列表成功')
      this.orderList = data.data
      console.log(this.orderList)
    },
    //  分页处理函数
    handleSizeChange(val) {
      this.getOrderParams.pagesize = val
      this.getOrderLists()
    },
    handleCurrentChange(val) {
      this.getOrderParams.pagenum = val
      this.getOrderLists()
    },
    //  点击修改按钮
    modOrderBtn(row) {
      this.getOrderId(row.order_id)
      this.modOrderDialog = true
    },
    //  根据id获取获取订单详情
    async getOrderId(id) {
      const { data } = await this.$http.get(`orders/${id}`)
      if (data.meta.status !== 200) {
        return this.$message.error('根据id获取订单列表失败')
      }
      // this.$message.success('根据id获取订单列表成功')
      console.log('根据id获取订单列表成功')
      this.orderID = data.data.order_id
      const isSend = data.data.is_send
      const orderPay = data.data.order_pay
      const orderPrice = data.data.order_price
      const orderNumber = data.data.order_number
      const payStatus = data.data.pay_status
      if (isSend === '是') {
        this.is_send = true
      } else {
        this.is_send = false
      }
      if (payStatus === '1') {
        this.pay_status = true
      } else {
        this.pay_status = false
      }
      this.orderListID.order_price = orderPrice
      this.orderListID.order_number = orderNumber
      switch (orderPay) {
        case '0':
          this.orderListID.order_pay = '0'
          break
        case '1':
          this.orderListID.order_pay = '1'
          break
        case '2':
          this.orderListID.order_pay = '2'
          break
        case '3':
          this.orderListID.order_pay = '3'
          break
        default:
          break
      }
      console.log(isSend)
      console.log(this.orderListID)
      console.log(data.data)
    },
    //  提交表单事件
    async subModOrder() {
      this.orderListID.is_send = this.is_send ? 1 : 0
      this.orderListID.pay_status = this.pay_status ? 1 : 0
      console.log(this.orderListID)
      const { data } = await this.$http.put(
        `orders/${this.orderID}`,
        this.orderListID
      )
      console.log(data)
      if (data.meta.status !== 201) {
        return this.$message.error('修改订单失败')
      }
      this.$message.success('修改订单成功')
      this.modOrderDialog = false
      this.getOrderLists()
    },
    //  搜索订单
    searchOrderBtn() {
      console.log(1)
      this.getOrderParams.query = this.searchOrder
      this.getOrderLists()
    },
    //  获取快点信息
    async kuaidiOrderBtn(row) {
      this.kuaidiDialog = true
      const { data } = await this.$http.get('/kuaidi/1106975712662')
      console.log(data)
    }
  }
}
</script>

<style lang="less" scoped></style>
